<?php
namespace app\jiyun\controller\base;

use app\common\model\base\CountryModel;
use app\common\model\jiyun\base\ChannelModel;
use app\jiyun\controller\BaseController;
use app\jiyun\service\base\ConfigService;
use think\admin\helper\QueryHelper;

class Channel extends BaseController
{
    /**
     * 渠道列表
     * @auth true
     * @menu true
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index ()
    {

        ChannelModel::mQuery()->layTable(function () {
            $this->title = '渠道列表';

        }, function (QueryHelper $query) {

            $query->with(['country','express','account']);
            // 数据列表搜索过滤
            $query->like('title')->dateBetween('create_at');
        });
    }


    protected function _index_page_filter (&$data)
    {

        foreach ($data as &$item) {
            $item['account_name'] =  $item['account']['title'] ?? '';
            $item['express_name'] =  $item['express']['alias_name'] ?? '';
            $item['country_name'] =  $item['country']['coun_c_name'] ?? '';
        }

    }

    /**
     * 添加渠道
     * @auth true
     * @return void
     */
    public function add ()
    {
        ChannelModel::mForm();
    }

    /**
     * 编辑渠道
     * @auth true
     * @return void
     */
    public function edit ()
    {
        ChannelModel::mForm();
    }


    protected function _form_filter (&$data)
    {

        if ($this->request->isGet()) {


            $account_id = $data['account_id'] ?? $this->account_id;
            $this->express_arr  = ConfigService::instance()->getExpress($account_id);
            $this->country_arr  = CountryModel::mk()->where('1=1')->column('coun_c_name','country_code');
        }
    }

    /**
     * 修改用户状态
     * @auth true
     */
    public function state()
    {

        ChannelModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除系统用户
     * @auth true
     */
    public function remove()
    {

        ChannelModel::mDelete();
    }

}
?>